﻿using System;
using System.Data;
using System.Linq;
using System.Windows;
using WPFExcelReport.Chart;
using System.Windows.Controls;
using WPFExcelReport.Helpers;
using System.ComponentModel;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class UITSTVKP : UIVKPOS
    {
        public UITSTVKP()
        {
            MESSAGES += "VKKNEW;VVRELOAD;VVTCHG;VKPCHK;WDCHG;"; 
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e); 
        }

        public override void VKKLOAD(DataRow _dwVKK)
        {
            if (KRS.Length < 1)
                return;
            base.VKKLOAD(_dwVKK);
            SResult = TMV.VVVKP(KRS, LFD, LIEFSCH);
            MergeData(SResult, true);
            if (LIEFSCH == "")
            {
                if (TBMain.Rows.Count == 0)
                    NewCommand(null, null);
            }
        }

        public override void PRODREQ()
        {
            switch (ARTST)
            {
                case "F":
                    break;
            }
        }

        public override void CheckDelDate(DataRow dwData)
        {
            if (app.company != "UBT")
            {
                base.CheckDelDate(dwData);
                dwData["VKPLIEFTERM"] = Helper.GetDateAddDays(MV.date, 30);
            }
            else
            {
                if (TMV.VKPLIEFTERM.ToString().Length != 8)
                {
                    base.CheckDelDate(dwData);
                    if (app.company != "UBT")
                    {
                        dwData["VKPLIEFTERM"] = Helper.GetDateAddDays(MV.date, 30);
                    }
                }
            }

        }

        public override void Insert(DataRow _dwVKP)
        {
            if (!IsCheckVA)
            {
                switch(app.company)
                {
                    case "SJH":
                        _dwVKP["VKPKOLL"] = "SJH17";
                        break;
                    default:
                        _dwVKP["VKPKOLL"] = POSTYP.PadRight(5);
                        break;
                }              
                _dwVKP["VKPART"] = (_dwVKP["VKPAUFKRS"].ToString() + _dwVKP["VKPAUFLFD"].ToString()).PadRight(24);
                _dwVKP["VKPFARBE"] = _dwVKP["VKPAUFPOS"].ToString().PadRight(10);
            }
            base.Insert(_dwVKP);
        }

        public override void OnEdit()
        {
            if (!IsEdit)
                return;
            if (TBTemp == null)
                TBTemp = TBMain.Copy();
            else
            {
                TBTemp.Clear();
                TBTemp.Merge(TBMain);
            }
            base.OnEdit();
        }
        public override void OnMEEHChg()
        {
            base.OnMEEHChg();
            /*
           if (MEEH.Trim() == "M" || MEEH.Trim() == "Y")
            {
                IsMTR = true;
                IsKG = false;
            }
            else
            {
                IsKG = true;
                IsMTR = false;
            }
             */
        }
        public override void OnSave()
        {
            if (!IsSaved)
                return;
            if (TMV.VKPLIEFTERM > 0)
            {
                if (TMV.VKPLIEFTERM.ToString().Trim().Length < 8)
                {
                    MessageBox.Show("日期格式为YYYYMMDD如（20150101），请修正！");
                    return;
                }
            }
            filter = "VKPMENGE1=0";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            bool IsChg = false;
            string info= Helpers.Helper.ConvertGB2312To8859P1("未定义更改原因");
            PM["EVENTID"] = "MVKPCHG";
            foreach (DataRow dwvp in TBMain.Rows)
            {
                filter = "VKPAUFPOS='" + dwvp["VKPAUFPOS"].ToString() + "'";
                rows = TBTemp.Select(filter);
                if (rows.Length > 0)
                {
                    if (dwvp.RowState == DataRowState.Unchanged) IsChg = false;
                    else
                    {
                        IsChg = true;//有更改
                        if(rows[0]["VKPART"].ToString()!= dwvp["VKPART"].ToString())
                        {
                            info = Helpers.Helper.ConvertGB2312To8859P1(rows[0]["VKPART"].ToString().Trim()+"品种更改:"+ dwvp["VKPART"].ToString().Trim());
                            PM["EVENTID"] = "MVKPCHGART";
                        }
                        double yqty = double.Parse(rows[0]["VKPMENGE1"].ToString().Trim()) * 0.01;
                        double xqty = double.Parse(dwvp["VKPMENGE1"].ToString().Trim()) * 0.01;
                        if (Math.Abs((xqty-yqty)/yqty)>0.01)
                        {
                            info += Helpers.Helper.ConvertGB2312To8859P1(yqty+"数量更改:超过了10%，为"+ xqty);
                            PM["EVENTID"] = "MVKPCHGQTY";
                        }
                    }
                }
                else
                {
                    IsChg = true;//新行 
                }
                if (IsChg)
                {
                    UPDATEVKP(dwvp);//计算价值 
                    PM["CUSER"] = app.User;                    
                    PM["dwInput"] = dwMaster;
                    PM["KRS"] = dwvp["VKPAUFKRS"];
                    PM["LFD"] = dwvp["VKPAUFLFD"];
                    PM["POS"] = dwvp["VKPAUFPOS"];
                    PM["MSG"] = new MessageInfo();
                    PM["INFO"] = info;
                    WFNEW = null;
                    WFNEW = PM;
                    IsNewWF = true;
                    IsNewWF = false;
                }
            }
            TBMain.AcceptChanges();
            base.OnSave();
        }
    }
}
